package com.info3k.server.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.info3k.client.domain.Address;
import com.info3k.client.domain.Location;


@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class AddressP implements Serializable {

	private static final long serialVersionUID = -1873799364997737422L;
 
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;
    
    @Persistent
	private String address;
    
    @Persistent
    private String houseNumber;
    
    @Persistent
    private Double lat;
    
    @Persistent
    private Double lon;
    
    @Persistent
    private Set<Key> locations;
    
    @Persistent
    private Key user;
    
    public Set<Key> getLocations() {
		return locations;
	}

	public void setLocations(Set<Key> locations) {
		this.locations = locations;
	}
    
    public AddressP() {
    	
    }
    
    public AddressP(Address address) {
    	this.address = address.getAddress();
    	this.houseNumber = address.getHouseNumber();
    	if(address.getId()!=null)
    		this.setId(KeyFactory.createKey(AddressP.class.getSimpleName(), address.getId()));
    	else 
    		this.setId(KeyFactory.createKey(AddressP.class.getSimpleName(), System.currentTimeMillis()));
    	this.lat = address.getLat();
    	this.lon = address.getLon();
    	this.locations = new HashSet<Key>();
    	if(address.getLocations()!=null){
	    	for (Location loc : address.getLocations()) {
				Key k = KeyFactory.createKey(LocationP.class.getSimpleName(), loc.getId());
				this.locations.add(k);
			}
    	}
    	this.setUser(KeyFactory.createKey(UserP.class.getSimpleName(), address.getUser().getId()));
    }
    
    public static Address createAddress(AddressP addressP){
		Address address = new Address();
		address.setId(addressP.getId().getId());
		address.setAddress(addressP.getAddress());
		address.setHouseNumber(addressP.getHouseNumber());
		address.setLat(addressP.getLat());
		address.setLon(addressP.getLon());
		address.setLocations(new ArrayList<Location>());
		return address;
    }

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getHouseNumber() {
		return houseNumber;
	}

	public void setHouseNumber(String houseNumber) {
		this.houseNumber = houseNumber;
	}

	public Double getLat() {
		return lat;
	}

	public void setLat(Double lat) {
		this.lat = lat;
	}

	public Double getLon() {
		return lon;
	}

	public void setLon(Double lon) {
		this.lon = lon;
	}

	public void setId(Key id) {
		this.id = id;
	}

	public Key getId() {
		return id;
	}

	public void setUser(Key user) {
		this.user = user;
	}

	public Key getUser() {
		return user;
	}
}
